package com.terry.bootdemo.web.service.serviceimpl;

import static com.terry.bootdemo.web.common.SystemUtils.*;//导入此类所有的静态方法

import com.terry.bootdemo.web.common.SystemException;
import com.terry.bootdemo.web.dao.UserRepository;
import com.terry.bootdemo.web.entity.UserEntity;
import com.terry.bootdemo.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserRepository userRepository;

    @Override
    public UserEntity Login(String account, String password) throws Exception {
        $(account,"账号不能为空");
        $(password,"密码不能为空");
        UserEntity ue=userRepository.findByuserName(account);
        if(ue==null){
            throw  new SystemException("输入账户不存在");
        }
        if(!password.equals(ue.getPassword())){
            throw  new SystemException("输入密码错误");
        }
        return ue;
    }

    @Override
    public UserEntity FindEntityByPhone(String phoneNum) throws Exception {
        UserEntity u=userRepository.findByPhone(phoneNum);
        return u;
    }

    @Override
    @Transactional
    public void AddUser(UserEntity userEntity) throws Exception {
        userRepository.save(userEntity);
    }

    @Override
    public UserEntity findByuserName(String userName) throws Exception {
        UserEntity u= userRepository.findByuserName(userName);
        return u;
    }
    /*@Override
    public void save(UserEntity ue) {
        userRepository.save(ue);
    }*/
}